// pages/menu/region/locationDevice/addDevice/addDevice.js
var app = getApp()
var verify = require('../../../../../utils/verify-token.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    deviceIdentifier:"",
    size: 10,
    loading: true,
    filterValue: "",
    isFresh: false,
    deviceList:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var locationid = options.locationid
    this.setData({
      sh: app.globalData.screenHeight,
      sw: app.globalData.screenWidth,
      locationid: locationid
    })
    this.search();
  },
  
  // 监控input输入
  searchInput:function(res){
    console.log("searchInput", res.detail.value)
    this.setData({
      deviceIdentifier: res.detail.value
    })
  },

  // 重置size
  clearSize:function(){
    this.setData({
      size:10
    })
    this.search()
  },

  // 点位管理查询设备分页
  search:function(){
    var size = this.data.size
    var deviceId = this.data.deviceId
    var deviceIdentifier = this.data.deviceIdentifier
    var companyId = app.globalData.companyId
    var baseUrl = app.globalData.baseUrl
    var token = app.globalData.token
    var that = this

    console.log("size", size)
    console.log("deviceIdentifier", deviceIdentifier)

    wx.request({
      url: baseUrl + 'searchDeviceInLocation?page=0&size=' + size + '&companyId=' + companyId + '&deviceIdentifier=' + deviceIdentifier,
      header: {
        'AUTHORIZATION': token
      },
      success(res) {
        if (res.data.code == 60006) {
          verify.clearAll()
        } else if (res.data.code == 200) {
          console.log("deviceList", res.data.data.content)
          var deviceList = res.data.data.content
          var totalElements = res.data.data.totalElements
          var loading = false
          if (totalElements > 10) {
            loading = true
          }
          that.setData({
            deviceList: deviceList,
            totalElements: totalElements,
            loading: loading,
            isFresh: false
          })
        } else {
          wx.showModal({
            title: '操作失败',
            content: res.data.message,
          })
        }
        
      }
    })
  },

  // 设备加入点位
  add:function(res){
    var locationname = res.currentTarget.dataset.locationname
    var deviceid = res.currentTarget.dataset.deviceid
    var locationid = this.data.locationid
    var baseUrl = app.globalData.baseUrl
    var token = app.globalData.token
    var size = this.data.size
    var that = this
    console.log(locationname)
    console.log(locationid)
    console.log(deviceid)
    if (locationname=="无"){
      wx.showModal({
        title: '提示',
        content: '确定要将该设备加入点位？',
        success(res) {
          if (res.confirm) {
            console.log("用户点击确定")
            wx.request({
              url: baseUrl+'location/'+locationid+'/device',
              method:'put',
              header: {
                'AUTHORIZATION': token
              },
              data:{
                "devices":[deviceid]
              },
              success(res) {
                console.log("添加：",res)
                if (res.data.code == 60006) {
                  verify.clearAll()
                } else if(res.data.code==200){
                  wx.showToast({
                    title: '添加成功!',
                    duration: 3500
                  })
                  that.clearSize()
                } else {
                  wx.showModal({
                    title: '保存失败',
                    content: res.data.message,
                  })
                }
              }
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    }else{
      wx.showModal({
        title: '该设备已属于某个点位！',
        content: '请将该设备移除所属点位再绑定新的点位！',
      })
    }
  },  

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var size = this.data.size * 1 + 10
    var loading = this.data.loading
    var totalElements = this.data.totalElements
    this.setData({
      isFresh: true
    })
    if (size - 10 > totalElements) {
      this.setData({
        loading: false
      })
    } else {
      this.setData({
        size: size
      })
      this.search()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})